package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingLevel;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author alexis
 * @date 2024-11-03
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        // 1.根据护理计划id查询护理计划基本信息
        NursingPlan nursingPlan = nursingPlanMapper.selectById(id);

        // 2.属性拷贝
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);

        // 2.根据护理计划id查询关联的护理项目列表
        List<NursingProjectPlanVo> nursingProjectPlans = nursingProjectPlanMapper.selectListByPlanId(id);

        // 3.组装结果数据并返回
        nursingPlanVo.setProjectPlans(nursingProjectPlans);

        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        // 1.保存护理计划基本信息
        NursingPlan nursingPlan = new NursingPlan();

        // 2.对象属性拷贝
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        // mybatisPlus的保存方法保存后，也会返回主键
        nursingPlanMapper.insert(nursingPlan);

        // 3.批量保存护理计划和护理项目的对应关系
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();

        // 4.补全护理计划id
        projectPlans.forEach(nursingProjectPlan -> nursingProjectPlan.setPlanId(nursingPlan.getId()));

        int result = nursingProjectPlanMapper.batchSave(projectPlans);

        // 5.删除缓存
        deleteCache();

        // 6.返回结果
        return result > 0 ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        // 1.修改护理计划基本信息
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlanMapper.updateById(nursingPlan);

        // 删除缓存
        deleteCache();

        // 2.删除当前护理计划关联的所有护理项目
        nursingProjectPlanMapper.deleteByPlanId(nursingPlanDto.getId());

        // 3.批量保存护理计划和护理项目的对应关系
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();

        // 4.补全护理计划id
        projectPlans.forEach(nursingProjectPlan -> nursingProjectPlan.setPlanId(nursingPlan.getId()));

        int result = nursingProjectPlanMapper.batchSave(projectPlans);

        return result > 0 ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        // 1.批量删除护理计划关联的护理项目
        nursingProjectPlanMapper.batchDeleteByPlanIds(ids);
        // 2.删除护理计划
        boolean result = removeByIds(Arrays.asList(ids));
        // 3.删除缓存
        deleteCache();

        return result ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        // 1.删除数据
        boolean result = removeById(id);
        // 2.删除缓存
        deleteCache();
        // 3.返回结果
        return result ? 1 : 0;
    }

    private void deleteCache() {
        redisTemplate.delete(CacheConstants.NURSING_PLAN_ALL);
    }

    /**
     * 查询所有护理计划
     *
     * @return 护理计划列表
     */
    @Override
    public List<NursingPlan> getAllNursingPlans() {
        // 1.先查询redis中有没有对应的缓存
        String allNursingPlans = redisTemplate.opsForValue().get(CacheConstants.NURSING_PLAN_ALL);
        // 2.如果有，直接返回redis中缓存的数据
        if (StringUtils.isNotEmpty(allNursingPlans)) {
            // 转成List返回结果
            return JSONUtil.toList(allNursingPlans, NursingPlan.class);
        }

        // 3.如果缓存中没有，再查询数据库，并将结果添加到缓存中
        LambdaQueryWrapper<NursingPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NursingPlan::getStatus, 1);
        List<NursingPlan> nursingPlans = list(queryWrapper);
        // 将结果添加到缓存
        redisTemplate.opsForValue().set(CacheConstants.NURSING_PLAN_ALL, JSONUtil.toJsonStr(nursingPlans));

        return nursingPlans;
    }
}
